package ysq.xyg.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.Custom;
import ysq.xyg.model.Register;
import ysq.xyg.model.Store;
import ysq.xyg.model.UseDo;
import ysq.xyg.model.info.BaseUserInfo;
import ysq.xyg.model.info.RegisterInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.CustomService;
import ysq.xyg.service.RegisterService;
import ysq.xyg.service.StoreService;
import ysq.xyg.service.UseDoService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:15
 **/
@Controller
@RequestMapping("/register")
public class RegisterController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(RegisterController.class);
    @Resource
    private RegisterService registerService;
    @Resource
    private CustomService customService;
    @Resource
    private UseDoService useDoService;
    @Resource
    private StoreService storeService;
    private Register register;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response){
        List<Store> storeList=storeService.list(new Store());
        return new ModelAndView("/registerList").addObject("storeList",storeList);
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject JsonList( HttpServletRequest request,Register register, Page page) throws MalformedURLException, UnsupportedEncodingException {
        register.setRemoveTag("N");
        PageInfo<Register> pageInfo=registerService.pageLeft(register,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        jsonObject.put("data",pageInfo.getList());
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView SelectEntity(HttpServletRequest request,String id){
        ModelAndView view=new ModelAndView("/registerUpdateOrSave");
        Register register=registerService.selectByPrimaryKey(id);
        List<Store> storeList=storeService.list(new Store());
        List<Custom> customList=customService.list(new Custom("2"));
        view.addObject("customList",customList);
        //1、超级总管理员，2、财务号，3、管理员
        int kind=acceptCurrentUserKind(request);
        view.addObject("kind",kind);
        view.addObject("storeList",storeList);
        view.addObject("register",register);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject UpdateOrAddEntity(HttpServletRequest request,RegisterInfo registerInfo){
       BaseUserInfo baseUserInfo=acceptCurrentUser(request);
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(registerInfo.getId()==null || "".equals(registerInfo.getId())){
            register=new Register();
            register.setId(BaseUtil.uId());
            register.setCreateTime(new Date());
            register.setCustomId(registerInfo.getCustomId());
            register.setWangWang(registerInfo.getWangWang());
            register.setOrderNum(registerInfo.getOrderNum());
            register.setWeChat(registerInfo.getWeChat());
            register.setKeyWord(registerInfo.getKeyWord());
            register.setUrl(registerInfo.getUrl());
            register.setAddress(registerInfo.getAddress());
            register.setMoney(registerInfo.getMoney());
            register.setRemoveTag("N");
            register.setGift(registerInfo.getGift());
            register.setEwm(registerInfo.getEwm());
            register.setReturnBank(registerInfo.getReturnBank());
            register.setReturnTime(registerInfo.getReturnTime());
            register.setRemark(registerInfo.getRemark());
            register.setUpdateTime(new Date());
            register.setMonth(registerInfo.getMonth());
            register.setStoreId(registerInfo.getStoreId());
            register.setBackMoney(registerInfo.getBackMoney());
            if(StringUtil.isNotEmpty(registerInfo.getReturnDate())){
                register.setReturnTime(DateUtil.convertStringToDate(registerInfo.getReturnDate()));
            }
            int id=registerService.insert(register);
            if(id <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }else{
                useDoService.insert(new UseDo(baseUserInfo.getUserid(),"添加注册信息["+register.toString()+"]",BaseConstant.USE_DO_TYPE_ADD_REG));
            }
        }else{
            register=registerService.selectByPrimaryKey(registerInfo.getId());
            register.setCustomId(registerInfo.getCustomId());
            register.setWangWang(registerInfo.getWangWang());
            register.setOrderNum(registerInfo.getOrderNum());
            register.setWeChat(registerInfo.getWeChat());
            register.setKeyWord(registerInfo.getKeyWord());
            register.setUrl(registerInfo.getUrl());
            register.setAddress(registerInfo.getAddress());
            register.setMoney(registerInfo.getMoney());
            register.setGift(registerInfo.getGift());
            register.setEwm(registerInfo.getEwm());
            register.setUpdateTime(new Date());
            register.setReturnBank(registerInfo.getReturnBank());
            register.setReturnTime(registerInfo.getReturnTime());
            register.setRemark(registerInfo.getRemark());
            register.setMonth(registerInfo.getMonth());
            register.setBackMoney(registerInfo.getBackMoney());
            if(StringUtil.isNotEmpty(registerInfo.getReturnDate())){
                register.setReturnTime(DateUtil.convertStringToDate(registerInfo.getReturnDate()));
            }
            register.setStoreId(registerInfo.getStoreId());
            int id=registerService.updateByPrimaryKey(register);
            if(id <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }else{
                useDoService.insert(new UseDo(baseUserInfo.getUserid(),"修改注册信息["+register.toString()+"]",BaseConstant.USE_DO_TYPE_UPDATE_REG));
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject DeleteEntity(HttpServletRequest request, String id){
        BaseUserInfo baseUserInfo=acceptCurrentUser(request);
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            Register register=registerService.selectByPrimaryKey(id);
            register.setRemoveTag("Y");
            int num=registerService.updateByPrimaryKey(register);
            if(num <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }else{
                useDoService.insert(new UseDo(baseUserInfo.getUserid(),"删除注册信息["+register.toString()+"]",BaseConstant.USE_DO_TYPE_DELETE_REG));
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
    @RequestMapping(value = "/export")
    public void getExport( HttpServletRequest request,HttpServletResponse response, Register register){
        register.setRemoveTag("N");
        registerService.export(response,register);
    }
}
